//
//  DUDayDetailView.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/21.
//

import Cocoa

@IBDesignable
class DUDayDetailView: NSView , DUDayDetailProtocol{
    
    private var background: DULabel!
    private var date: DULabel!
    private var day: DULabel!
    private var dayBack: DULabel!
    private var lunarYear: DULabel!
    private var lunarMonthDay: DULabel!
    private var context: DULabel!
    private var daysBack:DULabel!
    private var days: DULabel!
    private var weeksback:DULabel!
    private var weeks: DULabel!
    private var festivalBack: DULabel!
    private var festivalTitle: DULabel!
    private var festival1Value: FestivalView!
    private var festival2Value: FestivalView!
    private var festival3Value: FestivalView!
    private var setUpButton: SetUpButton!

    convenience init() {
        self.init(frame: CGRect.zero)
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    override func layout() {
        super.layout()
        dayBack.layer?.cornerRadius = 8
        daysBack.layer?.cornerRadius = 5
        weeksback.layer?.cornerRadius = 5
        background.setFrameSize(NSSize(width: self.frame.width, height: self.frame.height))
    }
    
    private func initialize () {
        background = DULabel()
        backAttribute(label: background, x: 0, y: 0, w: self.frame.width, h: self.frame.height)
        background.backgroundColor = NSColor(red: 0.3, green: 0.43, blue: 0.95, alpha: 0.9)

        date = DULabel()
        labelAttribute(label: date, y: 382, size: 18)
        date.stringValue = "2021-10-28"
        
        dayBack = DULabel()
        backAttribute(label: dayBack, x: 11, y: 272, w: 100, h: 100)
        
        day = DULabel()
        labelAttribute(label: day, y: 272, size: 60)
        day.frame = NSRect(x: 11, y: 272, width: 100, height: 100)
        day.stringValue = "28"
        
        
        lunarYear = DULabel()
        labelAttribute(label: lunarYear, y: 239, size: 18)
        lunarYear.stringValue = "辛丑(牛)年"
        
        lunarMonthDay = DULabel()
        labelAttribute(label: lunarMonthDay, y: 211, size: 18)
        lunarMonthDay.stringValue = "闰九月初十"
        
        context = DULabel()
        labelAttribute(label: context, y: 183, size: 12)
        context.stringValue = "今年已过78.63%"
        
        daysBack = DULabel()
        backAttribute(label: daysBack, x: 3, y: 148, w: 56, h: 35)
        
        days = DULabel()
        labelAttribute(label: days, y: 148, size: 12)
        days.frame = NSRect(x: 1, y: 148, width: 60, height: 35)
        days.stringValue = "第888天"
        
        weeksback = DULabel()
        backAttribute(label: weeksback, x: 63, y: 148, w: 56, h: 35)
        
        weeks = DULabel()
        labelAttribute(label: weeks, y: 148, size: 12)
        weeks.frame = NSRect(x: 61, y: 148, width: 60, height: 35)
        weeks.stringValue = "第45周"

        festivalBack = DULabel()
        backAttribute(label: festivalBack, x: 0, y: 0, w: 122, h: 145)
        
        festivalTitle = DULabel()
        labelAttribute(label: festivalTitle, y: 117, size: 18)
        festivalTitle.frame = NSRect(x: 0, y: 115, width: self.frame.width, height: 35)
        festivalTitle.textColor = NSColor(red: 0.15, green: 0.15, blue: 0.15, alpha: 0.9)
        festivalTitle.stringValue = "当日事件"
        
        festival1Value = FestivalView(frame: NSRect(x: 0, y: 87, width: self.frame.width, height: 28))
        addSubview(festival1Value)
        festival2Value = FestivalView(frame: NSRect(x: 0, y: 59, width: self.frame.width, height: 28))
        addSubview(festival2Value)

        festival3Value = FestivalView(frame: NSRect(x: 0, y: 31, width: self.frame.width, height: 28))
        addSubview(festival3Value)

        setUpButton = SetUpButton(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 30))
        addSubview(setUpButton)
        
    }
    
    private func labelAttribute (label:DULabel, y:CGFloat, size:CGFloat) {
        label.frame = NSRect(x: 0, y: y, width: self.frame.width, height: 28)
        label.cell = VerticallyCenteredTextFieldCell(textCell: "88")
        label.font = NSFont.systemFont(ofSize: size)
        label.textColor = NSColor(red: 1, green: 1, blue: 1, alpha: 0.9)
        label.alignment = .center
        addSubview(label)
    }
    
    private func backAttribute(label:DULabel,x:CGFloat, y:CGFloat, w:CGFloat, h:CGFloat) {
        label.frame = NSRect(x: x, y: y, width: w, height: h)
        label.backgroundColor = NSColor(red: 1, green: 1, blue: 1, alpha: 0.4)
        label.drawsBackground = true
        label.stringValue = ""
        addSubview(label)
    }
    
    func update(date: DateEntity) {
        self.date.stringValue = date.solar.formatterYearMonthDay()
        self.day.stringValue = String(date.solar.day)
        self.lunarYear.stringValue = date.lunar.lunarYear
        self.lunarMonthDay.stringValue = date.lunar.lunarMonthDay
        
        let n = CalendarUtil.dateCalculate(solar: date.solar)
        
        if n == 0 {
            self.context.stringValue = "今年已过" + String(format: "%.2f", CalendarUtil.getPastTime(solar: date.solar)) + "%"
        } else {
            let val = n > 0 ? "\(n)天后" : "\(-n)天前"
            self.context.stringValue = val
        }
        self.days.stringValue = "第\(CalendarUtil.whatDay(solar: date.solar))天"
        self.weeks.stringValue = "第\(date.solar.weekOfYear)周"
    }
}
